package com.baomidou;

import org.apache.commons.io.FileUtils;

import javax.servlet.http.HttpServletResponse;
import javax.swing.plaf.TableHeaderUI;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.List;
import java.util.Map;

public class Picture {

    public static void main(String[] args) throws MalformedURLException, InterruptedException {
        String id = "27507";
        String url = "https://pic.netbian.com/downpic.php?id=" + id + "&classid=";

        for (int i = 0; i < 10000; i++) {
            System.out.println(doGet(url + i));
            System.out.println(i+"          _________________________________________");
            Thread.sleep(1);
        }


    }



    public static String doGet(String httpUrl) {
        //链接
        HttpURLConnection connection = null;
        InputStream is = null;
        BufferedReader br = null;
        StringBuffer result = new StringBuffer();
        try {
            //创建连接
            URL url = new URL(httpUrl);
            System.out.println(httpUrl);
            connection = (HttpURLConnection) url.openConnection();


            //设置请求方式
            connection.setRequestMethod("GET");

            //设置通用的请求属性
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
            connection.setRequestProperty("Content-Type", "application/json;charset=utf-8");
            connection.setRequestProperty("Cookie", "__yjs_duid=1_1b4bef6557f5f3ec8294743e3ac519f11631495846889; " +
                                                    "Hm_lvt_526caf4e20c21f06a4e9209712d6a20e=1631495888; " +
                                                    "Hm_lpvt_526caf4e20c21f06a4e9209712d6a20e=1631612353; " +
                                                    "PHPSESSID=mq6lo9f0p5e1dbuf0mjramdsk3; " +
                                                    "zkhanecookieclassrecord=%2C66%2C; " +
                                                    "zkhanmlusername=qq910745163152; " +
                                                    "zkhanmluserid=5420000; " +
                                                    "zkhanmlgroupid=1; " +
                                                    "zkhanmlrnd=pYGGYk9id6j8VBASUPmf; " +
                                                    "zkhanmlauth=d2fd9707175ec3ab5a26a1578f8f6344");

            //设置连接超时时间
            connection.setReadTimeout(15000);
            //开始连接
            connection.connect();

            //获取响应数据
            if (connection.getResponseCode() == 200) {
                String contentType = connection.getContentType();
                System.out.println(contentType);
                //判断http Content-type: 是否为 image/jpg
                if (contentType.equals("image/jpg")) {
                    String headerField = connection.getHeaderField("Content-Disposition");
                    String str1 = headerField.substring(0, headerField.indexOf("="));
                    String str2 = headerField.substring(str1.length()+2,headerField.length()-1);
                    String fileName = URLDecoder.decode(str2, "UTF-8");



                    is = connection.getInputStream();
                    boolean b = downloadImg(is, "G:\\picture\\" + fileName);
                    result.append("成功");
                }
                else {
                    //获取返回的数据
                    is = connection.getInputStream();
                    if (null != is) {
                        br = new BufferedReader(new InputStreamReader(is,"GBK"));
                        String temp = null;
                        while (null != (temp = br.readLine())) {
                            result.append(temp);
                        }
                    }
                }



            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != br) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != is) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            //关闭远程连接
            connection.disconnect();
        }
        return result.toString();
    }

    public static boolean downloadImg(InputStream inputStream, String path){
        boolean flag = true;
        File file = new File(path);
        if (file.exists()){
            return flag;
        }
        File fileParent = file.getParentFile();
        if (!fileParent.exists()){
            fileParent.mkdirs();//创建路径
        }
        try {
            FileUtils.copyToFile(inputStream,file);
        }catch (Exception e) {
            e.printStackTrace();
            flag = false;
        }
        return flag;
    }
}
